package com.cn.itcast.gjp.controller;

import com.cn.itcast.gjp.domain.Ledger;
import com.cn.itcast.gjp.mapper.LedgerMapper;
import com.cn.itcast.gjp.mapper.SortMapper;
import com.cn.itcast.gjp.view.AbstractOperationLedgerDialog;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.util.List;

public class EditLedgerController extends AbstractOperationLedgerDialog {
    private Ledger tableLedger;

    public EditLedgerController(JDialog dialog, Ledger ledger) {
        super(dialog);
        this.setTitle("编辑账务");
        titleLabel.setText("编辑账务") ;

        tableLedger = ledger;

        parentBox.setSelectedItem(tableLedger.getParent());
        sortBox.setSelectedItem(tableLedger.getSname());
        accountTxt.setText(tableLedger.getAccount());
        moneyTxt.setText(tableLedger.getMoney() + "");
        createtimeTxt.setText(tableLedger.getCreatetime());
        ldescTxt.setText(tableLedger.getLdesc());
    }

    @Override
    public void changeParent() {
        SqlSession session= MyBatisUtil.getSqlSession();
        SortMapper sortMapper = session.getMapper(SortMapper.class);


        String parent = this.parentBox.getSelectedItem().toString();
        if(parent.equals("-请选择-")  ) {
            Object[] sortItems = {"-请选择-"};
            sortBox.setModel(new DefaultComboBoxModel(sortItems));
        } else   {
            List<String> nameItems = sortMapper.getSnameByParent(parent);
            nameItems.add(0, "-请选择-") ;
            sortBox.setModel(new DefaultComboBoxModel(nameItems.toArray())) ;
        }

        session.close();
    }

    @Override
    public void confirm() {
        SqlSession session= MyBatisUtil.getSqlSession();
        LedgerMapper ledgerMapper = session.getMapper(LedgerMapper.class);
        SortMapper sortMapper = session.getMapper(SortMapper.class);

        //判断
        String parent = parentBox.getSelectedItem().toString();
        if(parent.equals("-请选择-")) {
            JOptionPane.showMessageDialog(this, "\"收/支\"不能为空！");
            return;
        }
        String sname = sortBox.getSelectedItem().toString();
        if(sname.equals("-请选择-")) {
            JOptionPane.showMessageDialog(this, "\"分类\"不能为空！");
            return;
        }
        String account = accountTxt.getText();
        if(account.isEmpty()) {
            JOptionPane.showMessageDialog(this, "\"账户\"不能为空！");
            return;
        }
        double money = 0;
        try {
            money = Double.parseDouble(moneyTxt.getText());
        } catch(NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "\"账户\"不能为空！");
            return;
        }
        String createtime = createtimeTxt.getText();
        String desc = ldescTxt.getText();
        //赋值
        tableLedger.setParent(parent);
        tableLedger.setAccount(account);
        tableLedger.setMoney(money);
        tableLedger.setCreatetime(createtime);
        tableLedger.setLdesc(desc);

        int sid = sortMapper.getSidBySname(sname);
        tableLedger.setSid(sid) ;
        ledgerMapper.EditLedger(tableLedger);

        this.dispose();
        session.commit();
        session.close();

    }
}
